package com.dexin.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author: 酷酷的鑫
 * @Date: 2021/9/28 14:21
 * @description:
 */
@Component
public class SendMailUtil{
    // spring 提供的邮件发送类
    @Autowired
    JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String username;

    public void sendSimpleMail(String recName,String topic,String content)  {
        SimpleMailMessage message = new SimpleMailMessage();
        // 发送者
        message.setFrom(username);
        // 接受者
        message.setTo(recName);
        // 邮件主题
        message.setSubject(topic);
        // 邮件内容
        message.setText(content);
        mailSender.send(message);
    }

    public void sendTemp(String to,String topic) throws MessagingException {
        //发送带附件邮件内容
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setFrom(username);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject(topic);
        mimeMessageHelper.setText("<p style='color:red'> 123 </p>",true);
        //发送附件
        mimeMessageHelper.addAttachment("123.txt", new File("D:\\新建文本文档.txt"));
//        mimeMessageHelper.addAttachment("123.rar", new File("D:\\资产池管理平台.rar"));
        //发送邮件
        mailSender.send(mimeMessage);
    }
}
